<?php
/**
 * 功能: 图标管理
 * User: 文丹
 * Date: 2018/7/9 0009
 * Time: 16:11
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;

class Icon extends Power{
    protected $beforeActionList = [
        'first'=> ['except'=>'index,icon_add'],

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 图标展示列表
     */
    public function index(){
        $icons = Db::name('icon')
            ->alias('i')
            ->join('icon_type it','i.icon_type_id = it.id')
            ->field('i.*,it.name as type_name')
            ->paginate(10);
        $count = Db::name('icon')->count();

        $this->assign([
            'icons'=>$icons,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="8">暂无数据</td></tr>'
        ]);
        return  view('');
    }

    /**
     * 添加页面
     */
    public function icon_add(){
        $id = intval(input('id'));
        if($id){
            $icon = Db::name('icon')->where('id',$id)->find();
            $this->assign([
                'icon'=>$icon
            ]);
        }

        $type  = Db::name('icon_type')->select();
        $this->assign([
            'type'=>$type
        ]);
        return  view('');
    }
    public function icon_hold(){
        $name  = trim(input('name'));
        $link  = trim(input('link'));
        $image = trim(input('image'));
        $icon_type_id = trim(input('icon_type_id'));
        $is_goods = intval(input('is_goods'));

        if(empty($name)||empty($image)){
            return json(['msg'=>'请填写完整的数据！','ok'=>false]);
        }
        $data = [
            'name'=>$name,
            'link'=>$link,
            'icon_type_id'=>$icon_type_id,
            'is_goods'=>$is_goods,
            'logo'=>$image
        ];
        $result =  $result =  Db::name('icon')->insert($data);
        if($result){
            return json(['msg'=>'添加成功！','ok'=>true]);
        }else{
            return json(['msg'=>'添加失败！','ok'=>false]);
        }
    }

    /**
     * 图标修改
     */
    public function icon_edit(){
        $id    = intval(input('id'));
        $name  = trim(input('name'));
        $link  = trim(input('link'));
        $image = trim(input('image'));
        $icon_type_id = trim(input('icon_type_id'));
        $is_goods = intval(input('is_goods'));

        if(empty($name)||empty($image)){
            return json(['msg'=>'请填写完整的数据！','ok'=>false]);
        }
        $data = [
            'name'=>$name,
            'link'=>$link,
            'icon_type_id'=>$icon_type_id,
            'is_goods'=>$is_goods,
            'logo'=>$image
        ];
        $result =  $result =  Db::name('icon')->where('id',$id)->update($data);
        if($result){
            return json(['msg'=>'修改成功！','ok'=>true]);
        }else{
            return json(['msg'=>'修改失败！','ok'=>false]);
        }
    }
    /**
     * 图标下架
     */
    public function down(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
        $result = Db::name('icon')->where('id',$id)->update(['is_down'=>2]);
        if(!empty($result)){
            return json(['msg'=>'已下架！','ok'=>true]);
        }else{
            return json(['msg'=>'下架失败！','ok'=>false]);
        }
    }
    /**
     * 图标发布
     */
    public function up(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
        $result = Db::name('icon')->where('id',$id)->update(['is_down'=>1]);
        if(!empty($result)){
            return json(['msg'=>'已下架！','ok'=>true]);
        }else{
            return json(['msg'=>'下架失败！','ok'=>false]);
        }
    }
    public function icon_del(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数！','ok'=>false]);
        }
        $result = Db::name('icon')->where('id',$id)->delete();
        if(!empty($result)){
            return json(['msg'=>'已删除！','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败！','ok'=>false]);
        }
    }

}